﻿using MediatR;
using RearLivestock.Beginning.Api.Application.Command.ScionChange;
using RearLivestock.Common.Domains.hhx;
using RearLivestock.Common.Domains.wbx;
using RearLivestock.Common.Dtos;
using RearLivestock.Common.Infrastructure.Interface;
using RearLivestock.RBAC.ErrorCodeEnum;

namespace RearLivestock.Beginning.Api.Application.Command.Echarts
{
    public class GetVarietiesHandler : IRequestHandler<GetVarietiesCommand, Result>
    {
        /// <summary>
        /// 品种仓储
        /// </summary>
        private readonly IBaseRepository<Varieties> varietiesRepository;
        /// <summary>
        /// 基本信息仓储
        /// </summary>
        private readonly IBaseRepository<BasicInformation> basicInformationrepository;
        /// <summary>
        /// 公猪期初仓储
        /// </summary>
        private readonly IBaseRepository<Infancies> infanciesRepository;
        /// <summary>
        /// 日志
        /// </summary>
        private readonly ILogger<AddScionChangeHandler> logger;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="varietiesRepository"></param>
        /// <param name="basicInformationrepository"></param>
        /// <param name="logger"></param>
        public GetVarietiesHandler(IBaseRepository<Varieties> varietiesRepository, IBaseRepository<BasicInformation> basicInformationrepository, IBaseRepository<Infancies> infanciesRepository,ILogger<AddScionChangeHandler> logger)
        {
            this.varietiesRepository = varietiesRepository;
            this.basicInformationrepository = basicInformationrepository;
            this.infanciesRepository = infanciesRepository;
            this.logger = logger;
        }
        /// <summary>
        /// 业务逻辑
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<Result> Handle(GetVarietiesCommand request, CancellationToken cancellationToken)
        {
            var result = new Result() { Code = (int)CommonErrorCode.Search };
            try
            {
                var list = from a in (await basicInformationrepository.GetAll())
                           join b in (await varietiesRepository.GetAll())
                           on a.VarietiesId equals b.VarietiesId
                           join c in (await infanciesRepository.GetAll())
                           on a.BasicInforId equals c.BasicInforId
                           group new { a, b,c } by new { b.VarietiesName } into d
                           select new PigCount()
                           {
                               name=d.Key.VarietiesName,
                               value=d.Count()
                           };
                result.Code = (int)CommonErrorCode.Success;
                result.Data = list;
                result.Msg = "获取种猪只品种分组信息成功";
            }
            catch (Exception ex)
            {
                result.Msg = "获取种猪只品种分组信息异常" + ex.Message;
                logger.LogError("获取种猪只品种分组信息异常" + ex.Message);
            }
            return result;
        }
    }
}
